package com.resttest.ljs.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.LifecycleState;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Controller
@Slf4j
public class JsController {

    @PostMapping("/jieshou")
    @ResponseBody
    public String jieshou(String xtdm, @RequestBody JSONObject jsonObject){
        try {
            log.info("接收报文：{}",jsonObject.toJSONString());
            log.info("=====>>"+xtdm);
            log.info("返回报文：{}","收到");
            return "成功";
        }catch (Exception e){
            return "失败";
        }
    }


    @PostMapping("/csyb")
    @ResponseBody
    public String cs(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","1");
        jsonObject.put("msg","成功");

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            /*try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("execute里"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        });
        executorService.shutdown();
        System.out.println(jsonObject.toJSONString()+"当前时间："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        return jsonObject.toJSONString()+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
    }

    public static void ceshi() throws Exception {
        List list=null;
        Optional.ofNullable(list).orElseThrow(() -> new Exception("123"));
        /*try {
            Optional.ofNullable(list).orElseThrow(() -> new Exception("123"));
        }catch (Exception e){
            System.out.println("异常了");
            return;
        }*/
        System.out.println("业务逻辑");
    }

    public static void main(String[] args) throws Exception {
        try {
            ceshi();
            System.out.println("1");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            System.out.println("2");
        }
        System.out.println("3");
    }


}
